package com.nbcb.fund.entity.dto;

import com.nbcb.fund.entity.code.FundCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

@Data
@ApiModel(description = "统一分页返回结果")
public class FundPageResult<T> implements Serializable {

    @ApiModelProperty("错误代码 0:成功  其他为异常")
    private int code = 0;

    @ApiModelProperty("错误详细信息")
    private String message = "成功";

    @ApiModelProperty("返回的数据，调用失败的是，返回数据为null")
    private T data;

    @ApiModelProperty("总条数")
    private long total;

    @ApiModelProperty("页数")
    private long pageNo;

    @ApiModelProperty("单页条数")
    private long pageSize;


    public FundPageResult(FundCode fundCode) {
        this.code = fundCode.getCode();
        this.message = fundCode.getMessage();
    }

    /**
     * 成功
     *
     * @return FundPageResult
     */
    public static FundPageResult success() {
        return new FundPageResult(FundCode.SUCCESS);
    }

    /**
     * 成功带参数
     *
     * @param data     数据<p>List</p>
     * @param total    总数
     * @param pageNo   页数
     * @param pageSize 单页数据
     * @param <T>      数据格式
     * @return FundPageResult
     */
    public static <T> FundPageResult success(T data, long total, long pageNo, long pageSize) {
        FundPageResult fundPageResult = new FundPageResult(FundCode.SUCCESS);
        fundPageResult.setData(data);
        fundPageResult.setTotal(total);
        fundPageResult.setPageNo(pageNo);
        fundPageResult.setPageSize(pageSize);
        return fundPageResult;
    }

    /**
     * 系统异常
     *
     * @return FundPageResult
     */
    public static FundPageResult fail() {
        FundPageResult fundResult = new FundPageResult(FundCode.FAIL);
        return fundResult;
    }
}
